﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Model;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem.OGWell
{
    public partial class Diqu_Manage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PM_Filter();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Diqu_Manage));
        }
        
        [AjaxPro.AjaxMethod]
        public bool SaveDiqu(object dqbm, object dqmc)
        {
            try
            {
                Xiaov.SmartWeb.Model.ActiveRecord.BM_DQ model = new Model.ActiveRecord.BM_DQ();
                model.DQBM = dqbm.ToString();
                model.DQMC = dqmc.ToString();
                model.Save();
                return true;
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        [AjaxPro.AjaxMethod]
        public bool DeleteDiqu(object dqbm)
        {
            try
            {
                var q = Xiaov.SmartWeb.Model.ActiveRecord.BM_DQ.TryFind(dqbm);
                if (q != null)
                {
                    if (Xiaov.SmartWeb.Model.ActiveRecord.DJJCXX.FindAllByProperty("DQBM", q.DQBM).Count() > 0)
                        throw new Exception("这个地区在单井信息表中使用过，不能删除");
                    else
                        q.Delete();
                }
                return true;
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetNewId()
        {
            string bm = "DQ";
            object maxid = Maticsoft.DBUtility.DbHelperOra.GetSingle("select max(dqbm) from bm_dq");
            if (maxid.ToRequestString() == "")
            {
                return bm + "00";
            }
            else
            {
                string maxnum = maxid.ToString().Substring(maxid.ToString().Length - 2, 2);
                if (maxnum.Substring(0, 1) == "0")
                {
                    int x = maxnum.ToString().Substring(1, 1).ToInt32() + 1;
                    if (x >= 10)
                    {
                        return bm + x.ToString();
                    }
                    else
                    {
                        return bm + "0" + x.ToString();
                    }
                }
                else
                {
                    int x = maxnum.ToInt32() + 1;
                    return bm + x.ToString();
                }
            }
        }
    }
}